package org.walkerljl.commons.image.group;

import org.walkerljl.commons.io.FileUtils;
import org.walkerljl.commons.io.ZipUtils;
import org.walkerljl.commons.log.Logger;
import org.walkerljl.commons.log.LoggerFactory;

import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;

/**
 * GroupProducer
 *
 * @author lijunlin
 */
public class GroupProducer implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(GroupProducer.class);

    private String srcImagesPath;
    private BlockingQueue<File> taskQueue;
    private CountDownLatch finishLatch;

    public GroupProducer(String srcImagesPath, BlockingQueue<File> taskQueue, CountDownLatch finishLatch) {
        this.srcImagesPath = srcImagesPath;
        this.taskQueue = taskQueue;
        this.finishLatch = finishLatch;
    }

    @Override
    public void run() {
        try {
            run0(new File(srcImagesPath));
        } catch (Throwable e) {
            LOGGER.error(e);
        } finally {
            finishLatch.countDown();
        }
    }

    private void run0(File file) throws Exception {
        if (file.isFile()) {
            String fileSuffix = file.getPath().toLowerCase();
            if (fileSuffix.endsWith(".zip")) {
                String unzipTempDirectoryPath = srcImagesPath + File.separator + "unzip" + File.separator;
                FileUtils.mkdirs(unzipTempDirectoryPath);
                File unzipTempDirectory = new File(unzipTempDirectoryPath);
                ZipUtils.unzip(file, unzipTempDirectory);
                unzipTempDirectory = new File(unzipTempDirectoryPath);
                if (unzipTempDirectory == null) {
                    return;
                }
                File[] unzipFiles = unzipTempDirectory.listFiles(new ImageGroupFileFilter());
                if (unzipFiles == null || unzipFiles.length == 0) {
                    return;
                }
                for (File unzipFile : unzipFiles) {
                    run0(unzipFile);
                }
            } else if (fileSuffix.endsWith(".jpg")) {
                taskQueue.put(file);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(String.format("[%s]Process one task:%s.",
                            Thread.currentThread().getName(), file.getAbsolutePath()));
                }
            } else {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(String.format("[%s]Invalid image format:%s.",
                            Thread.currentThread().getName(), file.getAbsolutePath()));
                }
            }
        } else if (file.isDirectory()) {
            File[] subFiles = file.listFiles(new ImageGroupFileFilter());
            if (subFiles == null || subFiles.length == 0) {
                return;
            }
            for (File subFile : subFiles) {
                run0(subFile);
            }
        }
    }
}

class ImageGroupFileFilter implements FileFilter {

    @Override
    public boolean accept(File file) {
        String suffix = file.getPath().toLowerCase();
        return suffix.endsWith("") || suffix.endsWith(".jpg") || suffix.endsWith(".zip");
    }
}
